package zb.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zb.blog.base.bean.Result;
import zb.blog.base.global.GlobalVar;
import zb.blog.base.sessionScope.SessionScope;
import zb.blog.bean.NoticeBean;
import zb.blog.dao.NoticeDao;
import zb.blog.service.INoticeService;

import java.util.List;

@Service
public class NoticeService implements INoticeService {
    @Autowired
    private NoticeDao noticeDao;

    @Override
    public PageInfo<NoticeBean> getNoticeAll(Integer pageNum, Integer pageSize, String title, Integer enable, String startTime, String endTime) {
        if (pageNum == null) {
            pageNum = GlobalVar.PAGE_NUM;
        }
        if (pageSize == null) {
            pageSize = GlobalVar.NOTICE_PAGE_SIZE;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<NoticeBean> list = noticeDao.getNoticeAll(title, enable, startTime, endTime);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public Result updateStatusNoticeById(Integer id, Integer status) {
        Result res = new Result();
        if (status == 0 || status == 1) {//更新状态
            noticeDao.updateStatusNoticeById(id, status);
            res.setMsg("更新状态成功！");
        } else if (status == -1) {//直接删除
            noticeDao.deleteNoticeById(id);
            res.setMsg("删除公告成功！");
        }
        res.setSuccess(GlobalVar.SUCCESS);
        return res;
    }

    @Override
    public Result getNoticeById(Integer id) {
        Result res = new Result();
        NoticeBean noticeBean = noticeDao.getNoticeById(id);
        res.setData(noticeBean);
        res.setSuccess(GlobalVar.SUCCESS);
        return res;
    }

    @Override
    public Result editNotice(Integer id, String title, String content, String type, Integer enable) {
        Result res = new Result();
        Integer userId = SessionScope.getLoginAuth().getUser().getId();
        if (type != null && type.equals("edit") && id != null) {
            noticeDao.updateNotice(id, title, userId, content, enable);
            res.setSuccess(GlobalVar.SUCCESS);
            res.setMsg("更新公告成功！");
        } else if (type != null && type.equals("add")) {
            noticeDao.addNotice(title, userId, content, enable);
            res.setSuccess(GlobalVar.SUCCESS);
            res.setMsg("添加公告成功！");
        } else {
            res.setSuccess(GlobalVar.FAIL);
            res.setMsg("id不存在或无法是别的操作!");
        }
        return res;
    }
}
